﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using RaveEngine.Engine.Query.Translate;
using System.Xml;
using System.Xml.Serialization;
using RaveEngine.Engine.Query;

namespace RaveEngine.Engine.Config
{
    public class RaveKeyConfig
    {

        public BaseRaveConfig this[string k]
        {
            get
            {
                return dictConfigs[k];
            }            
        }

        protected Dictionary<string, BaseRaveConfig> dictConfigs = new Dictionary<string, BaseRaveConfig>();

        public RaveKeyConfig(string filePath)
        {
            FileInfo fi = new FileInfo(filePath);

            // No TRY/CATCH on purpose to let the file error bubble up to the app

            XmlDocument xDoc = new XmlDocument();
            XmlNodeList xList;
            raveTranslateConfig configTemp;
            XmlSerializer serializer = new XmlSerializer(typeof(raveTranslateConfig));

            using (FileStream fs = fi.OpenRead())
            {
                xDoc.Load(fs);
            }
            xList = xDoc.GetElementsByTagName("raveTranslateConfig");

            for (int i = 0; i < xList.Count; i++)
            {
                using (StringReader sr = new StringReader(xList[i].OuterXml))
                {
                    configTemp = (raveTranslateConfig)serializer.Deserialize(sr);
                    dictConfigs.Add(configTemp.key, configTemp);
                }

            }

       }
    }
}